Report post

How to quickly count the number of elements in a PHP array?

To quickly count the number of elements in a PHP array, we can simply use the count function: The count function is the recommended function to count the number of elements in a PHP array or a Countable object. Beside the count function, sizeof is an alternative function which will deliver the same result.

How to count a multi-dimensional array in PHP?

Try the PHP function array_count_values. If you want to count ALL the same occurences inside the array, here's a function to count them all, and return the results as a multi-dimensional array: array_count_values only works for integers and strings.

Is array_count_values() fast in PHP 5.6?

In PHP 5.6 the array_count_values() version is faster for arrays with less different elements. All in all both functions are quite fast (at least for the arrays I was testing) To count a value in a two dimensional array, here is the useful snippet to process and get count of a particular value - 1, 'userId' => 5],

The World's Leading Crypto Trading Platform

Get my welcome gifts